package com.cainaer.product.controller;


import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.utils.bean.BeanUtils;
import com.cainaer.common.core.web.controller.BaseController;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.product.domain.ProductCategory;
import com.cainaer.product.domain.vo.ProductCategoryVo;
import com.cainaer.product.service.IProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-09-11
 */
@RestController
@RequestMapping("/productCategory/")
public class ProductCategoryController extends BaseController {

    @Autowired
    IProductCategoryService productCategoryService;

    /**
     * 查询商品分类
     *
     * @param productCategory
     * @return
     */
    @GetMapping("list")
    public AjaxResult selectProductCategoryService(ProductCategory productCategory) {

        return AjaxResult.success(productCategoryService.selectProductCategories(productCategory));
    }

    /**
     * 新增分类
     *
     * @param productCategory
     * @return
     */
    @PostMapping("add")
    public AjaxResult insertProductCategory(@Validated @RequestBody ProductCategory productCategory) {
        logger.info("productCategory->{}",productCategory);
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    /**
     * 编辑分类
     *
     * @param productCategory
     * @return
     */
    @PostMapping("edit")
    public AjaxResult editProductCategory(@Validated @RequestBody ProductCategory productCategory) {
        if (StringUtils.isNull(productCategory.getCategoryId())){
            return AjaxResult.error("主键不能为空");
        }
        return toAjax(productCategoryService.editProductCategory(productCategory));
    }

    /**
     * 删除分类
     *
     * @param productCategoryVo
     * @return
     */
    @PostMapping("del")
    public AjaxResult delProductCategory(@Validated @RequestBody ProductCategoryVo productCategoryVo) {
        ProductCategory productCategory=new ProductCategory();
        BeanUtils.copyBeanProp(productCategory,productCategoryVo);
        return toAjax(productCategoryService.editProductCategory(productCategory));
    }
}
